package com.chanyachting.emall.action.club;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.domain.Dock;
import com.chanyachting.emall.form.DockForm;
import com.chanyachting.emall.service.ChannelService;
import com.chanyachting.emall.service.DockyachtService;
import com.chanyachting.emall.serviceImpl.ChannelServiceImpl;
import com.chanyachting.emall.serviceImpl.DockyachtServiceImpl;
import com.chanyachting.emall.util.PageUtil;

public class QueryDockyachtAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//一般页面记录数
		int COMMON_PAGE_SIZE = 20;

		DockForm dForm = (DockForm) form;
		DockyachtService dyservice=new DockyachtServiceImpl();
		ChannelService cService = new ChannelServiceImpl();
		
		int currentPage = dForm.getCurrentPage();
		int channelId = dForm.getChannelId();
		int pageSize = COMMON_PAGE_SIZE;
		if (dForm.getChannelId() == null) {
			return mapping.findForward("error");
		}
		
		List<Channel> subChannelList = cService.getSubChannel(new Channel(dForm.getChannelId()));
		if (subChannelList == null || subChannelList.size() == 0) {
			return mapping.findForward("error");
		}
		
		String channelStr = String.valueOf(channelId);
		
		for(Channel tmpC:subChannelList) {
			channelStr = channelStr + "," + tmpC.getId();
		}
		
		Dock tmpDock = null;
		String bearchNum = "";
		if (null != dForm.getDockKeyWord() && !"".equals(dForm.getDockKeyWord())) {
			tmpDock = new Dock();
			tmpDock.setTitle(dForm.getDockKeyWord());
			
			int minBerthNum = dForm.getMinBerthNum();
			int maxBerthNum = dForm.getMaxBerthNum();
			if(minBerthNum != 0  && maxBerthNum != 0 && maxBerthNum > minBerthNum) {
				bearchNum = "between " + minBerthNum + " and " + maxBerthNum;
			}
			if (null != dForm.getDockArea() && !"".equals(dForm.getDockArea())) {
				tmpDock.setAddress(dForm.getDockArea());
			}
			if (null != dForm.getBerthType() && !"".equals(dForm.getBerthType())) {
				tmpDock.setBerthType(dForm.getBerthType());
			}
		}

		int pageCount = 0;
		
		if (tmpDock == null) {
			pageCount = dyservice.QueryAllDockCountByChannelId(channelStr)/pageSize;
		} else {
			pageCount = dyservice.SearchDockCountByChannel(channelStr,tmpDock,bearchNum)/pageSize;
		}
		
		if (pageCount%pageSize > 0) {
			pageCount = pageCount + 1;
		}
		
		if (currentPage < 1) {
			currentPage = 1;
		} else if (currentPage > pageCount) {
			currentPage = 1;
		}
		dForm.setSubChannelName("");
		
		dForm.setSubChannelList(subChannelList);
		if (tmpDock == null) {
			dForm.setDockList(dyservice.QueryAllDockByChannelId(currentPage, pageSize,channelStr));
		} else {
			dForm.setDockList(dyservice.SearchDockByChannel(currentPage, pageSize,channelStr,tmpDock,bearchNum));
		}
		String toPageAct = PageUtil.getGotoAction(request.getRequestURL().toString(), request.getParameterMap());
		String linkStr = PageUtil.getFy3(pageCount, currentPage, toPageAct);
		String selectionStr = PageUtil.getFy4(pageCount, currentPage, toPageAct);
		dForm.setLinkStr(linkStr);
		dForm.setSelectionStr(selectionStr);
		
		return mapping.findForward("doReadDockyachtList");
	}

}
